home *** CD-ROM | disk | FTP | other *** search
/ MacHack 2000 / MacHack 2000.toast / pc / The Hacks / MacHacksBug / Python 1.5.2c1 / Lib / dis.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2000-06-23  |  8.5 KB  |  245 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 1.5)
  3.  
  4. '''Disassembler of Python byte code into mnemonics.'''
  5. import sys
  6. import string
  7. import types
  8.  
  9. def dis(x = None):
  10.     '''Disassemble classes, methods, functions, or code.
  11.  
  12. \tWith no argument, disassemble the last traceback.
  13.  
  14. \t'''
  15.     if not x:
  16.         distb()
  17.         return None
  18.     
  19.     if type(x) is types.InstanceType:
  20.         x = x.__class__
  21.     
  22.     if hasattr(x, '__dict__'):
  23.         items = x.__dict__.items()
  24.         items.sort()
  25.         for name, x1 in items:
  26.             pass
  27.         
  28.     elif hasattr(x, 'im_func'):
  29.         x = x.im_func
  30.     
  31.     if hasattr(x, 'func_code'):
  32.         x = x.func_code
  33.     
  34.     if hasattr(x, 'co_code'):
  35.         disassemble(x)
  36.     else:
  37.         raise TypeError, "don't know how to disassemble %s objects" % type(x).__name__
  38.  
  39.  
  40. def distb(tb = None):
  41.     '''Disassemble a traceback (default: last traceback).'''
  42.     if not tb:
  43.         
  44.         try:
  45.             tb = sys.last_traceback
  46.         except AttributeError:
  47.             raise RuntimeError, 'no last traceback to disassemble'
  48.  
  49.         while tb.tb_next:
  50.             tb = tb.tb_next
  51.     
  52.     disassemble(tb.tb_frame.f_code, tb.tb_lasti)
  53.  
  54.  
  55. def disassemble(co, lasti = -1):
  56.     '''Disassemble a code object.'''
  57.     code = co.co_code
  58.     labels = findlabels(code)
  59.     n = len(code)
  60.     i = 0
  61.     while i < n:
  62.         c = code[i]
  63.         op = ord(c)
  64.         if op == SET_LINENO and i > 0:
  65.             print 
  66.         
  67.         if i == lasti:
  68.             print '-->',
  69.         else:
  70.             print '   ',
  71.         if i in labels:
  72.             print '>>',
  73.         else:
  74.             print '  ',
  75.         print string.rjust(`i`, 4), string.ljust(opname[op], 15),
  76.         i = i + 1
  77.         if op >= HAVE_ARGUMENT:
  78.             oparg = ord(code[i]) + ord(code[i + 1]) * 256
  79.             i = i + 2
  80.             print string.rjust(`oparg`, 5),
  81.             if op in hasconst:
  82.                 print '(' + `co.co_consts[oparg]` + ')',
  83.             elif op in hasname:
  84.                 print '(' + co.co_names[oparg] + ')',
  85.             elif op in hasjrel:
  86.                 print '(to ' + `i + oparg` + ')',
  87.             elif op in haslocal:
  88.                 print '(' + co.co_varnames[oparg] + ')',
  89.             elif op in hascompare:
  90.                 print '(' + cmp_op[oparg] + ')',
  91.             
  92.         
  93.         print 
  94.  
  95. disco = disassemble
  96.  
  97. def findlabels(code):
  98.     '''Detect all offsets in a byte code which are jump targets.
  99.  
  100. \tReturn the list of offsets.
  101.  
  102. \t'''
  103.     labels = []
  104.     n = len(code)
  105.     i = 0
  106.     while i < n:
  107.         c = code[i]
  108.         op = ord(c)
  109.         i = i + 1
  110.         if op >= HAVE_ARGUMENT:
  111.             oparg = ord(code[i]) + ord(code[i + 1]) * 256
  112.             i = i + 2
  113.             label = -1
  114.             if op in hasjrel:
  115.                 label = i + oparg
  116.             elif op in hasjabs:
  117.                 label = oparg
  118.             
  119.             if label >= 0:
  120.                 if label not in labels:
  121.                     labels.append(label)
  122.                 
  123.             
  124.         
  125.     return labels
  126.  
  127. cmp_op = ('<', '<=', '==', '!=', '>', '>=', 'in', 'not in', 'is', 'is not', 'exception match', 'BAD')
  128. hasconst = []
  129. hasname = []
  130. hasjrel = []
  131. hasjabs = []
  132. haslocal = []
  133. hascompare = []
  134. opname = [
  135.     ''] * 256
  136. for op in range(256):
  137.     opname[op] = '<' + `op` + '>'
  138.  
  139.  
  140. def def_op(name, op):
  141.     opname[op] = name
  142.  
  143.  
  144. def name_op(name, op):
  145.     opname[op] = name
  146.     hasname.append(op)
  147.  
  148.  
  149. def jrel_op(name, op):
  150.     opname[op] = name
  151.     hasjrel.append(op)
  152.  
  153.  
  154. def jabs_op(name, op):
  155.     opname[op] = name
  156.     hasjabs.append(op)
  157.  
  158. def_op('STOP_CODE', 0)
  159. def_op('POP_TOP', 1)
  160. def_op('ROT_TWO', 2)
  161. def_op('ROT_THREE', 3)
  162. def_op('DUP_TOP', 4)
  163. def_op('UNARY_POSITIVE', 10)
  164. def_op('UNARY_NEGATIVE', 11)
  165. def_op('UNARY_NOT', 12)
  166. def_op('UNARY_CONVERT', 13)
  167. def_op('UNARY_INVERT', 15)
  168. def_op('BINARY_POWER', 19)
  169. def_op('BINARY_MULTIPLY', 20)
  170. def_op('BINARY_DIVIDE', 21)
  171. def_op('BINARY_MODULO', 22)
  172. def_op('BINARY_ADD', 23)
  173. def_op('BINARY_SUBTRACT', 24)
  174. def_op('BINARY_SUBSCR', 25)
  175. def_op('SLICE+0', 30)
  176. def_op('SLICE+1', 31)
  177. def_op('SLICE+2', 32)
  178. def_op('SLICE+3', 33)
  179. def_op('STORE_SLICE+0', 40)
  180. def_op('STORE_SLICE+1', 41)
  181. def_op('STORE_SLICE+2', 42)
  182. def_op('STORE_SLICE+3', 43)
  183. def_op('DELETE_SLICE+0', 50)
  184. def_op('DELETE_SLICE+1', 51)
  185. def_op('DELETE_SLICE+2', 52)
  186. def_op('DELETE_SLICE+3', 53)
  187. def_op('STORE_SUBSCR', 60)
  188. def_op('DELETE_SUBSCR', 61)
  189. def_op('BINARY_LSHIFT', 62)
  190. def_op('BINARY_RSHIFT', 63)
  191. def_op('BINARY_AND', 64)
  192. def_op('BINARY_XOR', 65)
  193. def_op('BINARY_OR', 66)
  194. def_op('PRINT_EXPR', 70)
  195. def_op('PRINT_ITEM', 71)
  196. def_op('PRINT_NEWLINE', 72)
  197. def_op('BREAK_LOOP', 80)
  198. def_op('LOAD_LOCALS', 82)
  199. def_op('RETURN_VALUE', 83)
  200. def_op('EXEC_STMT', 85)
  201. def_op('POP_BLOCK', 87)
  202. def_op('END_FINALLY', 88)
  203. def_op('BUILD_CLASS', 89)
  204. HAVE_ARGUMENT = 90
  205. name_op('STORE_NAME', 90)
  206. name_op('DELETE_NAME', 91)
  207. def_op('UNPACK_TUPLE', 92)
  208. def_op('UNPACK_LIST', 93)
  209. name_op('STORE_ATTR', 95)
  210. name_op('DELETE_ATTR', 96)
  211. name_op('STORE_GLOBAL', 97)
  212. name_op('DELETE_GLOBAL', 98)
  213. def_op('LOAD_CONST', 100)
  214. hasconst.append(100)
  215. name_op('LOAD_NAME', 101)
  216. def_op('BUILD_TUPLE', 102)
  217. def_op('BUILD_LIST', 103)
  218. def_op('BUILD_MAP', 104)
  219. name_op('LOAD_ATTR', 105)
  220. def_op('COMPARE_OP', 106)
  221. hascompare.append(106)
  222. name_op('IMPORT_NAME', 107)
  223. name_op('IMPORT_FROM', 108)
  224. jrel_op('JUMP_FORWARD', 110)
  225. jrel_op('JUMP_IF_FALSE', 111)
  226. jrel_op('JUMP_IF_TRUE', 112)
  227. jabs_op('JUMP_ABSOLUTE', 113)
  228. jrel_op('FOR_LOOP', 114)
  229. name_op('LOAD_GLOBAL', 116)
  230. jrel_op('SETUP_LOOP', 120)
  231. jrel_op('SETUP_EXCEPT', 121)
  232. jrel_op('SETUP_FINALLY', 122)
  233. def_op('LOAD_FAST', 124)
  234. haslocal.append(124)
  235. def_op('STORE_FAST', 125)
  236. haslocal.append(125)
  237. def_op('DELETE_FAST', 126)
  238. haslocal.append(126)
  239. def_op('SET_LINENO', 127)
  240. SET_LINENO = 127
  241. def_op('RAISE_VARARGS', 130)
  242. def_op('CALL_FUNCTION', 131)
  243. def_op('MAKE_FUNCTION', 132)
  244. def_op('BUILD_SLICE', 133)
  245.